package com.huangpuweb.task.controller;

import com.huangpuweb.task.model.QzJGWPARKData;
import com.huangpuweb.task.model.QzJGWPARKInfoData;
import com.huangpuweb.task.model.SysHpPark;
import com.huangpuweb.task.model.SysHpParkInfo;
import com.huangpuweb.task.service.SyncQzJGWDataService;
import com.huangpuweb.task.service.SyncSysDataService;
import com.huangpuweb.task.util.DateUtil;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * 定时同步停车场数据
 */
@Controller
public class SyncParkDataJob {

    private static Logger logger = Logger.getLogger(SyncParkDataJob.class);



    private SyncSysDataService syncSysDataService;
    @Autowired
    public void setSyncSysDataService(SyncSysDataService syncSysDataService) {
        this.syncSysDataService = syncSysDataService;
    }

    private SyncQzJGWDataService syncQzJGWDataService;
    @Autowired
    public void setSyncQzJGWDataService(SyncQzJGWDataService syncQzJGWDataService) {
        this.syncQzJGWDataService = syncQzJGWDataService;
    }

    public void SyncParkDataJobTask() throws Exception {

        LogFactory.getLog(this.getClass()).info("--------start-------SyncParkDataJobTask----------" + new Date());

        //获取前置库停车场数据
        List<QzJGWPARKData> listQzJGWPARKData=syncQzJGWDataService.getListQzJGWPARKData();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if(listQzJGWPARKData != null && listQzJGWPARKData.size()>0){

                for(QzJGWPARKData parkData:listQzJGWPARKData){
                    //存在该条停车场数据
                    boolean parkDataflag = syncSysDataService.checkParkData(parkData);

                    SysHpPark hp_park = new SysHpPark();
                    hp_park.setParkId(parkData.getPARKID());
                    hp_park.setParkName(parkData.getPARKNAME());
                    hp_park.setParkAddress(parkData.getADDR());
                    if(!parkDataflag){
                        syncSysDataService.insertHpPark(hp_park);
                    }

                    //获取前置库停车场实时数据
                    QzJGWPARKInfoData parkInfoData=syncQzJGWDataService.getListQzJGWPARKInfoData(parkData.getPARKID());

                    if (null != parkInfoData) {

                        SysHpParkInfo sysHpParkInfo=syncSysDataService.getSysHpParkInfo(parkInfoData.getPARKID());
                        if (null!=sysHpParkInfo){
                            //存在该条停车场详细信息，更新该条数据
                            sysHpParkInfo.setTotal(parkData.getTOTNUM());
                            sysHpParkInfo.setTotal_left(parkInfoData.getTOTAL_LEFT());
                            sysHpParkInfo.setParkingNumber(parkData.getTOTNUM() - parkInfoData.getTOTAL_LEFT());
                            sysHpParkInfo.setRecordTime(sdf.format(parkInfoData.getSENDTIME()));
                            sysHpParkInfo.setUpdateTime(DateUtil.getDateTime());
                            syncSysDataService.updateSysHpParkInfo(sysHpParkInfo);

                        }else{
                            //不存在该条停车场详细信息，新增该条数据到数据库
                            //不存在-插入
                            sysHpParkInfo =  new SysHpParkInfo();
                            sysHpParkInfo.setParkId(parkInfoData.getPARKID());
                            sysHpParkInfo.setTotal(parkData.getTOTNUM());
                            sysHpParkInfo.setTotal_left(parkInfoData.getTOTAL_LEFT());
                            sysHpParkInfo.setParkingNumber(parkData.getTOTNUM() - parkInfoData.getTOTAL_LEFT());
                            sysHpParkInfo.setRecordTime(sdf.format(parkInfoData.getSENDTIME()));
                            sysHpParkInfo.setUpdateTime(DateUtil.getDateTime());
                            sysHpParkInfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                            syncSysDataService.insertSysHpParkInfo(sysHpParkInfo);
                        }

                    }
                }
        }
        LogFactory.getLog(this.getClass()).info("--------end-------SyncParkDataJobTask----------" + new Date());

    }



}
